package jeconbond.experiment.market.equilibrium.generate;

import jeconbond.economic.IEconomicEngine;
import jeconbond.economic.agent.IEconomicProducer;
import jeconbond.economic.market.agent.service.PurchaseExceedResourcesAgent;
import jeconbond.economic.market.agent.SellInitialResourcesAgent;
import jeconbond.experiment.market.equilibrium.MooreExperimentSettings;
import jeconbond.experiment.market.equilibrium.process.IMooreExperimentContext;
import jeconbond.experiment.generate.IEngineInitialiser;
import laboratoryQ.experiment.process.IExperimentContext;

public class MooreEngineInitialiser implements IEngineInitialiser {
	@Override
	public void initialise(
			IEconomicEngine engine, 
			IEconomicProducer[] agents, 
			IExperimentContext iexperimentContext
	) {
		IMooreExperimentContext experimentContext =
			(IMooreExperimentContext) iexperimentContext;

		addSellInitialResource(
				engine, 
				experimentContext
		);
		engine.addAgents(agents);
		addPurchaseExceed(
				engine, 
				experimentContext
		);
		engine.addAgents(experimentContext.getServiceAgents());
	}

	private void addPurchaseExceed(
			IEconomicEngine engine,
			IMooreExperimentContext experimentContext
	) {
		MooreExperimentSettings settings = experimentContext.getExperimentSettings();
		if (settings.utiliseExceedPeriod > 0.0) {
			PurchaseExceedResourcesAgent purchaseExceedResourcesAgent = 
				new PurchaseExceedResourcesAgent(
						experimentContext.getRRS(),
						settings.utiliseExceedPeriod,
						settings.purchaseExceedDayBudget,
						settings.purchaseExceedPassivePeriod,
						settings.purchaseExceedUseTrackLength,
						settings.resources
				);
			engine.addAgents(
					purchaseExceedResourcesAgent
			);
			experimentContext.getRRS().addSellsListener(
					purchaseExceedResourcesAgent
			);
		}
	}

	private void addSellInitialResource(
			IEconomicEngine engine, 
			IMooreExperimentContext experimentContext
	) {
		MooreExperimentSettings settings = experimentContext.getExperimentSettings();
		SellInitialResourcesAgent sellInitialResourcesAgent = 
			new SellInitialResourcesAgent(
					experimentContext.getRRS(), 
					settings.resources, 
					settings.agentsCount, 
					settings.chainLength, 
					settings.initResourceExceedPercent
			);
		
		engine.addAgents(sellInitialResourcesAgent);
	}
}
